package dao

import (
	"week1/inits"
	"week1/model"
)

func AddArticle(title string, context string) (result *model.Article) {
	result = &model.Article{
		Title:   title,
		Context: context,
	}
	err := inits.DB.Create(&result).Error
	if err != nil {
		return nil
	}
	return result
}

func ArticleHistory(ArticleId int) (result *model.ArticleHistory) {
	result = &model.ArticleHistory{
		ArticleId: ArticleId,
	}
	err := inits.DB.Create(&result).Error
	if err != nil {
		return nil
	}
	return result
}

func ArticleLike(ArticleId int) (result *model.ArticleLike) {
	result = &model.ArticleLike{
		ArticleId: ArticleId,
	}
	err := inits.DB.Create(&result).Error
	if err != nil {
		return nil
	}
	return result
}

func AddArticleReview(ArticleId int, review string) (result *model.ArticleReview) {
	result = &model.ArticleReview{
		ArticleId: ArticleId,
		Review:    review,
	}
	err := inits.DB.Create(&result).Error
	if err != nil {
		return nil
	}
	return result
}

func ResArticleReview(ArticleId int, FatherId int, review string) (result *model.ArticleReview) {
	result = &model.ArticleReview{
		ArticleId: ArticleId,
		FatherId:  FatherId,
		Review:    review,
	}
	err := inits.DB.Create(&result).Error
	if err != nil {
		return nil
	}
	return result
}

func ReviewLike(reviewId int) (result *model.ReviewLike) {
	result = &model.ReviewLike{
		ReviewId: reviewId,
	}
	err := inits.DB.Create(&result).Error
	if err != nil {
		return nil
	}
	return result
}

func GetArticleId(id int) (result *model.Article) {
	err := inits.DB.Where("id=?", id).Find(&result).Error
	if err != nil {
		return nil
	}
	return result
}

func GetArticleReviewNum(ArticleId int) int {
	var count int64
	err := inits.DB.Model(&model.ArticleReview{}).Where("article_id=?", ArticleId).Count(&count).Error
	if err != nil {
		return 0
	}
	return int(count)
}
